import { ref } from 'vue'
import {
  faultTypeSave as faultTypeSaveAPI,
  getFaultTypeDetailById,
  getFaultTypes as getFaultTypesAPI,
  faultTypesDel as faultTypesDelAPI,
  faultTypesUpdateStatus,
} from '@/api/knowledge'

export const useFaultTypeForm = () => {
  // 故障分类保存加载
  const faultTypeSaveLoading = ref(false)

  // 故障分类对象
  const faultTypeForm = ref<KnownAPIRequest.KnowFaultTypeSave>({
    id: '',
    status: false,
    name: '',
    childData: [],
    parentId: '',
  })

  /**
   * 保存故障分类
   */
  const faultTypeSave = async () => {
    faultTypeSaveLoading.value = true
    const res = await faultTypeSaveAPI(faultTypeForm.value)
    faultTypeSaveLoading.value = false
    return res.data
  }

  /**
   * 重置故障分类表单
   */
  const resetFaultTypeForm = () => {
    faultTypeForm.value = {
      id: '',
      status: false,
      name: '',
      childData: [],
      parentId: '',
    }
  }

  return {
    faultTypeForm,
    faultTypeSaveLoading,
    faultTypeSave,
    resetFaultTypeForm,
  }
}

export const useGetFaultTypes = () => {
  // 获取故障分类加载
  const getFaultTypesLoading = ref(false)

  // 故障分类列表
  const faultTypes = ref<Array<KnowFaultTypes>>([])

  /**
   * 获取故障分类
   */
  const getFaultTypes = async () => {
    getFaultTypesLoading.value = true
    const res = await getFaultTypesAPI()
    getFaultTypesLoading.value = false
    faultTypes.value = res.data.data.rows
  }

  return {
    faultTypes,
    getFaultTypesLoading,
    getFaultTypes,
  }
}

export const useGetFaultTypeDetail = () => {
  // 获取故障分类详情加载
  const getFaultTypeDetailLoading = ref(false)

  // 故障分类详情
  const faultTypeDetail = ref<KnownFaultTypeDetail>({
    id: '',
    name: '',
    parentId: '',
    status: false,
    childData: [],
  })

  /**
   * 获取故障分类详情
   * @param {string} id - 故障分类id
   */
  const getFaultTypeDetail = async (id: string) => {
    getFaultTypeDetailLoading.value = true
    const res = await getFaultTypeDetailById(id)
    getFaultTypeDetailLoading.value = false
    faultTypeDetail.value = res.data.data.row
  }

  return {
    faultTypeDetail,
    getFaultTypeDetailLoading,
    getFaultTypeDetail,
  }
}

export const useFaltTypesDel = () => {
  // 故障分类批量删除加载
  const faultTypesDelLoading = ref(false)

  /**
   * 批量删除故障分类
   * @param {Array<string>} ids - 故障分类id集合
   */
  const faultTypesDel = async (ids: Array<string>) => {
    faultTypesDelLoading.value = true
    const res = await faultTypesDelAPI(ids)
    faultTypesDelLoading.value = false
    return res.data
  }

  return {
    faultTypesDelLoading,
    faultTypesDel,
  }
}

export const useFaultTypesEnable = () => {
  // 故障分类批量启用加载
  const faultTypesEnableLoading = ref(false)

  /**
   * 批量启用故障分类
   * @param {Array<string>} ids - 故障分类id集合
   */
  const faultTypesEnable = async (ids: Array<string>) => {
    faultTypesEnableLoading.value = true
    const res = await faultTypesUpdateStatus(ids, false)
    faultTypesEnableLoading.value = false
    return res.data
  }

  return {
    faultTypesEnableLoading,
    faultTypesEnable,
  }
}

export const useFaultTypesDisable = () => {
  // 故障分类批量禁用加载
  const faultTypesDisableLoading = ref(false)

  /**
   * 批量禁用故障分类
   * @param {Array<string>} ids - 故障分类id集合
   */
  const faultTypesDisable = async (ids: Array<string>) => {
    faultTypesDisableLoading.value = true
    const res = await faultTypesUpdateStatus(ids, true)
    faultTypesDisableLoading.value = false
    return res.data
  }

  return {
    faultTypesDisableLoading,
    faultTypesDisable,
  }
}
